import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;


public class Main {


    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("欢迎使用电脑配件库存管理系统!");
        menuSelect(scanner);
    }

    /**
     * 菜单选择
     * @param scanner 键盘输入
     */
    private static void menuSelect(Scanner scanner){
        System.out.println("请选择功能(输入数字)!");
        System.out.println("1.配件资料管理;2.进货、退货、出货管理;3.库存管理;4.信息统计;5.退出系统");
//        根据用户输入选择功能
        int functionType = scanner.nextInt();
        switch (functionType){
            case 1:
                queryInfoMenu(scanner);
                break;
            case 2:
                goodsMenu(scanner);
                break;
            case 3:
                goodsQueryMenu(scanner);
                break;
            case 4:
                staticsDataMenu(scanner);
                break;
            case 5:
                System.exit(0);
                break;
            default:break;
        }
    }

    /**
     * 配件管理
     * @param scanner
     */
    private static void queryInfoMenu(Scanner scanner){
        System.out.println("全部配件资料如下");
//        查询列表数据
        List<HashMap<String, String>> hashMaps = queryInfo();
        if (hashMaps == null || hashMaps.isEmpty()){
            System.out.println("暂无数据");
        }else{
            for (HashMap<String, String> hashMap : hashMaps) {
                String info = "配件编码:"+hashMap.get("code")+";配件名称:"+hashMap.get("name")+";品牌:"+hashMap.get("brand")+";价格:"+hashMap.get("price")+";";
                System.out.println(info);
            }
        }
        System.out.println("请选择功能(输入数字)!");
        System.out.println("1.新增;2.修改;3.删除;4.返回上一层菜单;5.退出系统");
//        根据键盘输入选择功能
        int type2 = scanner.nextInt();
        switch (type2){
            case 1:
                addInfoMenu(scanner);
                break;
            case 2:
                updateInfoMenu(scanner);
                break;
            case 3:
                deleteInfoMenu(scanner);
                break;
            case 4:
                menuSelect(scanner);
                break;
            case 5:
                System.exit(0);
                break;
            default:break;
        }
    }

    /**
     * 新增配件信息功能
     * @param scanner
     */
    private static void addInfoMenu(Scanner scanner){
        System.out.println("请输入配件编码:");
        String code = scanner.next();
        System.out.println("请输入配件名称:");
        String name = scanner.next();
        System.out.println("请输入品牌:");
        String brand = scanner.next();
        System.out.println("请输入价格:");
        String price = scanner.next();
//        新增
        addInfo(code,name,brand,price);
        System.out.println("新增成功");
        queryInfoMenu(scanner);
    }

    /**
     * 修改配件信息
     * @param scanner
     */
    public static void updateInfoMenu(Scanner scanner){
        System.out.println("请输入配件编码:");
        String code = scanner.next();
        System.out.println("请输入配件名称:");
        String name = scanner.next();
        System.out.println("请输入品牌:");
        String brand = scanner.next();
        System.out.println("请输入价格:");
        String price = scanner.next();
//        调用具体的修改方法
        updateInfo(code,name,brand,price);
        System.out.println("修改成功");
        queryInfoMenu(scanner);
    }

    /**
     * 删除配件信息功能
     * @param scanner
     */
    public static void deleteInfoMenu(Scanner scanner){
        System.out.println("请输入配件编码:");
        String code = scanner.next();
        deleteInfo(code);
        System.out.println("删除成功");
        queryInfoMenu(scanner);
    }

    /**
     * 查询所有资料
     * @return
     */
    private static List<HashMap<String,String>> queryInfo(){
        FileReader fileReader = null;
        BufferedReader bf = null;
        List<HashMap<String,String>> data = new ArrayList<>();
        try{
//            从文本文件中读取数据
            File file = new File("computeInfo.txt");
//            判断文件是否存在
            if (!file.exists()){
                file.createNewFile();
                return null;
            }
            fileReader = new FileReader("computeInfo.txt");
            bf = new BufferedReader(fileReader);
            String line=null;
//            遍历每一行文本,读取数据
            while ((line = bf.readLine())!=null){
                String[] lines = line.split(";");
                HashMap<String,String> item = new HashMap<>();
                item.put("code",lines[0]);
                item.put("name",lines[1]);
                item.put("brand",lines[2]);
                item.put("price",lines[3]);
                data.add(item);
            }
            return data;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (null != bf){
                    bf.close();
                    fileReader.close();
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return data;
    }

    /**
     * 添加资料
     * @param code 编码
     * @param name 名称
     * @param brand 品牌
     * @param price 价格
     */
    private static void addInfo(String code,String name,String brand,String price){
        FileWriter filew = null;
        BufferedWriter bw = null;
        try {
            boolean flag = false;
//            写入文件
            File file = new File("computeInfo.txt");
            if (!file.exists()){
                file.createNewFile();
                flag = true;
            }
            filew = new FileWriter(file,true);
            bw = new BufferedWriter(filew);
            if (file.length()==0){
                flag = true;
            }
            String info = code+";"+name+";"+brand+";"+price;
//            是否换行
            if (!flag){
                bw.newLine();
            }
            bw.write(info);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (null != bw){
                    bw.close();
                    filew.close();
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }

    /**
     * 修改资料
     * @param code 编码
     * @param name 名称
     * @param brand 品牌
     * @param price 价格
     */
    private static void updateInfo(String code,String name,String brand,String price){
        FileReader fileReader = null;
        BufferedReader bf = null;
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
//        利用备份文件修改数据
        File file = new File("computeInfo.txt");
        File file1 = new File("computeInfo_bak.txt");
        List<HashMap<String,String>> data = new ArrayList<>();
        try{
//            如果原始文件不存在,说明不存在数据,不允许修改
            if (!file.exists()){
                return ;
            }
            if (!file1.exists()){
                file1.createNewFile();
            }
            fileReader = new FileReader("computeInfo.txt");
            fileWriter = new FileWriter("computeInfo_bak.txt");
            bf = new BufferedReader(fileReader);
            bw = new BufferedWriter(fileWriter);
            String line=null;
//            遍历旧文件，写入备份文件
            while ((line = bf.readLine())!=null){
                String[] lines = line.split(";");
                HashMap<String,String> item = new HashMap<>();
//                找到要修改的数据,修改数据
                if (code.equals(lines[0])){
                    line = code+";"+name+";"+brand+";"+price;
                }
                bw.write(line);
                bw.newLine();
            }


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (bf!=null){
                    bf.close();
                    fileReader.close();
                    bw.close();
                    fileWriter.close();
//                    删除旧文件，重命名备份文件
                    file.delete();
                    file1.renameTo(new File("computeInfo.txt"));
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }

    /**
     * 删除资料
     * @param code
     */
    private static void deleteInfo(String code){
        FileReader fileReader = null;
        BufferedReader bf = null;
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
        File file = new File("computeInfo.txt");
        File file1 = new File("computeInfo_bak.txt");
        List<HashMap<String,String>> data = new ArrayList<>();
        try{

            if (!file.exists()){
                return ;
            }
            if (!file1.exists()){
                file1.createNewFile();
            }
            fileReader = new FileReader("computeInfo.txt");
            fileWriter = new FileWriter("computeInfo_bak.txt");
            bf = new BufferedReader(fileReader);
            bw = new BufferedWriter(fileWriter);
            String line=null;
            while ((line = bf.readLine())!=null){
                String[] lines = line.split(";");
                HashMap<String,String> item = new HashMap<>();
//                删除指定数据，即不往新文件中写入
                if (code.equals(lines[0])){
                    continue;
                }
                bw.write(line);
                bw.newLine();
            }

            file.delete();
            file1.renameTo(new File("computeInfo.txt"));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (bf != null){
                    bf.close();
                    fileReader.close();
                    bw.close();
                    fileWriter.close();
                    file.delete();
                    file1.renameTo(new File("computeInfo.txt"));
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    /**
     * 进出货管理功能
     * @param scanner
     */
    private static void goodsMenu(Scanner scanner){
        System.out.println("请选择功能(输入数字)!");
        System.out.println("1.进货;2.退货;3.出货;4.返回上一层菜单;5.退出系统");
        int type2 = scanner.nextInt();
//        根据用户输入选择功能
        switch (type2){
            case 1:
                buyGoodsMenu(scanner);
                break;
            case 2:
                backGoodsMenu(scanner);
                break;
            case 3:
                sellGoodsMenu(scanner);
                break;
            case 4:
                menuSelect(scanner);
                break;
            case 5:
                System.exit(0);
                break;
            default:break;
        }
    }

    /**
     * 进货
     * @param scanner
     */
    private static void buyGoodsMenu(Scanner scanner){
        System.out.println("请输入配件编码:");
        String code = scanner.next();
        System.out.println("请输入配件名称:");
        String name = scanner.next();
        System.out.println("请输入进货件数:");
        String num = scanner.next();
        System.out.println("请输入总价格:");
        String price = scanner.next();
        buyGoods(code,name,num,price);
        System.out.println("进货成功");
        goodsMenu(scanner);
    }

    /**
     * 具体的进货操作
     * @param code 编码
     * @param name 名称
     * @param num 数量
     * @param price 总价格
     */
    private static void buyGoods(String code,String name,String num,String price){
//        和上面的配件管理类似,利用备份文件完成更新操作
        FileReader fileReader = null;
        BufferedReader bf = null;
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
        File file = new File("goods.txt");
        File file1 = new File("goods_bak.txt");
        List<HashMap<String,String>> data = new ArrayList<>();
        try{
            if (!file.exists()){
                file.createNewFile();
            }
            if (!file1.exists()){
                file1.createNewFile();
            }
            fileReader = new FileReader("goods.txt");
            fileWriter = new FileWriter("goods_bak.txt");
            bf = new BufferedReader(fileReader);
            bw = new BufferedWriter(fileWriter);
            String line=null;
            boolean flag = false;
            while ((line = bf.readLine())!=null){
                String[] lines = line.split(";");
                HashMap<String,String> item = new HashMap<>();
//                找到对应的数据更新
                if (code.equals(lines[0])){
                    line = code+";"+name+";"+(Integer.parseInt(lines[2])+Integer.parseInt(num))+";"
                            +(Integer.parseInt(lines[3])+Integer.parseInt(price));
                    buyGoodsStatics(num);
                    flag = true;
                }
                bw.write(line);
                bw.newLine();
            }
//            如果数据不存在，则新增一条
            if (!flag){
                line = code+";"+name+";"+num+";"
                        +price;
                bw.write(line);
                buyGoodsStatics(num);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (bf != null){
                    bf.close();
                    fileReader.close();
                    bw.close();
                    fileWriter.close();
                    file.delete();
                    file1.renameTo(new File("goods.txt"));
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    /**
     * 退货
     * @param scanner
     */
    private static void backGoodsMenu(Scanner scanner){
        System.out.println("请输入配件编码:");
        String code = scanner.next();
        System.out.println("请输入配件名称:");
        String name = scanner.next();
        System.out.println("请输入退货件数:");
        String num = scanner.next();
        System.out.println("请输入总价格:");
        String price = scanner.next();
        boolean flag = backGoods(code, name, num, price);
        if (!flag){
            System.err.println("未找到对应的配件信息，请重新操作");
        }else{
            System.out.println("退货成功");
        }
        goodsMenu(scanner);
    }

    private static boolean backGoods(String code,String name,String num,String price){
        //        和上面的配件管理类似,利用备份文件完成更新操作
        FileReader fileReader = null;
        BufferedReader bf = null;
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
        File file = new File("goods.txt");
        File file1 = new File("goods_bak.txt");
        List<HashMap<String,String>> data = new ArrayList<>();
        try{
            if (!file.exists()){
                return false;
            }
            if (!file1.exists()){
                file1.createNewFile();
            }
            fileReader = new FileReader("goods.txt");
            fileWriter = new FileWriter("goods_bak.txt");
            bf = new BufferedReader(fileReader);
            bw = new BufferedWriter(fileWriter);
            String line=null;
            boolean flag = false;
            while ((line = bf.readLine())!=null){
                String[] lines = line.split(";");
//                找到对应的数据，进行更新
                if (code.equals(lines[0])){
                    line = code+";"+name+";"+(Integer.parseInt(lines[2])+Integer.parseInt(num))+";"
                            +(Integer.parseInt(lines[3])+Integer.parseInt(price));
                    backGoodsStatics(num);
                    flag = true;
                }
                bw.write(line);
                bw.newLine();
            }
            return flag;

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (bf != null){
                    bf.close();
                    fileReader.close();
                    bw.close();
                    fileWriter.close();
                    file.delete();
                    file1.renameTo(new File("goods.txt"));
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return false;

    }

    /**
     * 出货
     * @param scanner
     */
    private static void sellGoodsMenu(Scanner scanner){
        System.out.println("请输入配件编码:");
        String code = scanner.next();
        System.out.println("请输入配件名称:");
        String name = scanner.next();
        System.out.println("请输入出货件数:");
        String num = scanner.next();
        System.out.println("请输入总价格:");
        String price = scanner.next();

        boolean flag = sellGoods(code, name, num, price);
        if (!flag){
            System.err.println("库存不足，请先进货");
        }else{
            System.out.println("出货成功");
        }
        goodsMenu(scanner);
    }

    /**
     * 出货
     * @param code 编码
     * @param name 名称
     * @param num 数量
     * @param price 总价格
     * @return
     */
    private static boolean sellGoods(String code,String name,String num,String price){
//        和进货类似，都是利用复制文件内容到新文件完成修改
        FileReader fileReader = null;
        BufferedReader bf = null;
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
        File file = new File("goods.txt");
        File file1 = new File("goods_bak.txt");
        List<HashMap<String,String>> data = new ArrayList<>();
        try{
            if (!file.exists()){
                return false;
            }
            if (!file1.exists()){
                file1.createNewFile();
            }
            fileReader = new FileReader("goods.txt");
            fileWriter = new FileWriter("goods_bak.txt");
            bf = new BufferedReader(fileReader);
            bw = new BufferedWriter(fileWriter);
            String line=null;
            boolean flag = false;
            while ((line = bf.readLine())!=null){
                String[] lines = line.split(";");
                HashMap<String,String> item = new HashMap<>();
                if (code.equals(lines[0])){
//                    判断库存数是否充足
                    if (Integer.parseInt(lines[2]) > Integer.parseInt(num)){
                        line = code+";"+name+";"+(Integer.parseInt(lines[2])-Integer.parseInt(num))+";"
                                +(Integer.parseInt(lines[3])-Integer.parseInt(price));
                        sellGoodsStatics(num);
                        flag = true;
                    }else{
                        flag = false;
                        break;
                    }

                }
                bw.write(line);
                bw.newLine();
            }
            return flag;

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (bf != null){
                    bf.close();
                    fileReader.close();
                    bw.close();
                    fileWriter.close();
                    file.delete();
                    file1.renameTo(new File("goods.txt"));
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * 库存管理功能
     * @param scanner
     */
    private static void goodsQueryMenu(Scanner scanner){
        System.out.println("请选择功能(输入数字)!");
        System.out.println("1.查看库存;4.返回上一层菜单;5.退出系统");
        int type2 = scanner.nextInt();
        switch (type2){
            case 1:
                System.out.println("全部配件库存信息如下");
//                查询库存
                List<HashMap<String, String>> hashMaps = goodsQuery();
                if (null == hashMaps || hashMaps.isEmpty()){
                    System.out.println("暂无数据");
                }else{
                    for (HashMap<String, String> hashMap : hashMaps) {
                        String info = "配件编码:"+hashMap.get("code")+";配件名称:"+hashMap.get("name")+";库存数:"+hashMap.get("num")+";总价格:"+hashMap.get("price")+";";
                        System.out.println(info);
                    }
                }
                menuSelect(scanner);
                break;
            case 4:
                menuSelect(scanner);
                break;
            case 5:
                System.exit(0);
                break;
            default:break;
        }
    }

    /**
     * 库存查询
     * @return
     */
    private static List<HashMap<String,String>> goodsQuery(){
//        读取文件中的数据
        FileReader fileReader = null;
        BufferedReader bf = null;
        List<HashMap<String,String>> data = new ArrayList<>();
        try{
            File file = new File("goods.txt");
            if (!file.exists()){
                file.createNewFile();
                return null;
            }
            fileReader = new FileReader("goods.txt");
            bf = new BufferedReader(fileReader);
            String line=null;
//            遍历每一行数据
            while ((line = bf.readLine())!=null){
                String[] lines = line.split(";");
                HashMap<String,String> item = new HashMap<>();
                item.put("code",lines[0]);
                item.put("name",lines[1]);
                item.put("num",lines[2]);
                item.put("price",lines[3]);
                data.add(item);
            }
            return data;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (bf != null){
                    bf.close();
                    fileReader.close();
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return data;
    }

    /**
     * 信息统计功能
     * @param scanner
     */
    private static void staticsDataMenu(Scanner scanner){
        System.out.println("请选择功能(输入数字)!");
        System.out.println("1.查看统计信息;4.返回上一层菜单;5.退出系统");
        int type2 = scanner.nextInt();
        switch (type2){
            case 1:
                System.out.println("统计信息");
//                查询信息统计数据
                HashMap<String,String> data = staticsData();
                String statics = "配件总数:"+data.get("infoCount")+";库存总数:"+data.get("goodsCount")+";进货总数:"
                        +data.get("buyGoodsCount")+";退货总数:"+data.get("backGoodsCount")+"出货总数:"+data.get("sellGoodsCount");
                System.out.println("库存总数:"+statics);
                menuSelect(scanner);
                break;
            case 4:
                menuSelect(scanner);
                break;
            case 5:
                System.exit(0);
                break;
            default:break;
        }
    }

    /**
     * 查询信息统计数据
     * @return
     */
    private static HashMap<String,String> staticsData(){
//        查询配件资料
        List<HashMap<String,String>> infoList = queryInfo();
        HashMap<String,String> statics = new HashMap<>();
        statics.put("infoCount",String.valueOf(infoList.size()));
//        通过文件读取库存变动信息统计
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        File file = new File("statics.txt");
        try {
            boolean flag = false;
            if (!file.exists()){
                file.createNewFile();
                flag = true;
            }
            fileReader = new FileReader(file);
            bufferedReader = new BufferedReader(fileReader);
//            如果文件不存在，则初始化数据为0
            if (flag){
                statics.put("goodsCount","0");
                statics.put("buyGoodsCount","0");
                statics.put("backGoodsCount","0");
                statics.put("sellGoodsCount","0");

            }
            else{
                String line = bufferedReader.readLine();
                //            如果文件不存在，则初始化数据为0
                if (null == line){
                    statics.put("goodsCount","0");
                    statics.put("buyGoodsCount","0");
                    statics.put("backGoodsCount","0");
                    statics.put("sellGoodsCount","0");
                }
//                否则,就读取数据
                else{
                    String[] lines = line.split(";");
                    statics.put("goodsCount",lines[0]);
                    statics.put("buyGoodsCount",lines[1]);
                    statics.put("backGoodsCount",lines[2]);
                    statics.put("sellGoodsCount",lines[3]);
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (fileReader != null){
                    fileReader.close();
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return statics;
    }

    /**
     * 更新进货的统计数据
     * @param num 数量
     */
    private static void buyGoodsStatics(String num){
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        FileWriter filew = null;
//        同样的,利用文件复制操作更新数据
        File file = new File("statics.txt");
        File file1 = new File("statics_bak.txt");
        try {
            boolean flag = false;

            if (!file.exists()){
                file.createNewFile();
                flag = true;
            }
            if (!file1.exists()){
                file1.createNewFile();
            }
            fileReader = new FileReader(file);
            filew = new FileWriter(file1,true);
            bufferedReader = new BufferedReader(fileReader);
//            判断文件是否存在或者有数据，进行初始化赋值
            if (flag){
                String info = num+";"+num+";0;0";
                filew.write(info);
            }
            else{
                String line = bufferedReader.readLine();
                //            判断文件是否存在或者有数据，进行初始化赋值
                if (null == line){
                    String info = num+";"+num+";0;0";
                    filew.write(info);
                }
                // 完成数据更新
                else{
                    String[] lines = line.split(";");
                    String info = (Integer.valueOf(num)+Integer.parseInt(lines[0]))+";"+(Integer.valueOf(num)+Integer.valueOf(lines[1]))+";"+lines[2]+";"+lines[3];
                    filew.write(info);
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (filew != null){
                    filew.close();
                    fileReader.close();
                    file.delete();
                    file1.renameTo(new File("statics.txt"));
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    /**
     * 统计退货数据
     * @param num
     */
    private static void backGoodsStatics(String num){
//        和进货的操作类似
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        FileWriter filew = null;
        File file = new File("statics.txt");
        File file1 = new File("statics_bak.txt");
        try {
            boolean flag = false;

            if (!file.exists()){
                file.createNewFile();
                flag = true;
            }
            if (!file1.exists()){
                file1.createNewFile();
            }
            fileReader = new FileReader(file);
            filew = new FileWriter(file1,true);
            bufferedReader = new BufferedReader(fileReader);
            if (flag){
                String info = num+";0;"+num+";0";
                filew.write(info);
            }
            else{
                String line = bufferedReader.readLine();
                if (null == line){
                    String info = num+";0;"+num+";0";
                    filew.write(info);
                }else{
                    String[] lines = line.split(";");
                    String info = (Integer.parseInt(num)+Integer.parseInt(lines[0]))+";"+lines[1]+";"+(Integer.parseInt(num)+Integer.valueOf(lines[2]))+";"+lines[3];
                    filew.write(info);
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (filew != null){
                    filew.close();
                    fileReader.close();
                    file.delete();
                    file1.renameTo(new File("statics.txt"));
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

    /**
     * 统计出货数据
     * @param num
     */
    private static void sellGoodsStatics(String num){
//        和进货的操作类似
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        FileWriter filew = null;
        File file = new File("statics.txt");
        File file1 = new File("statics_bak.txt");
        try {
            boolean flag = false;

            if (!file.exists()){
                file.createNewFile();
                flag = true;
            }
            if (!file1.exists()){
                file1.createNewFile();
            }
            fileReader = new FileReader(file);
            filew = new FileWriter(file1,true);
            bufferedReader = new BufferedReader(fileReader);
            if (flag){
                String info = (0-Integer.parseInt(num))+";0;0;"+num;
                filew.write(info);
            }
            else{
                String line = bufferedReader.readLine();
                if (null == line){
                    String info = (0-Integer.parseInt(num))+";0;0;"+num;
                    filew.write(info);
                }else {
                    String[] lines = line.split(";");
                    String info = (Integer.parseInt(lines[0])-Integer.parseInt(num))+";"+lines[1]+";"+lines[2]+";"+(Integer.parseInt(num)+Integer.valueOf(lines[3]));
                    filew.write(info);
                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (filew != null){
                    filew.close();
                    fileReader.close();
                    file.delete();
                    file1.renameTo(new File("statics.txt"));
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }





}
